<!DOCTYPE html>
<html lang="en">
    <head>
        <% include('/common/common_header.html'){}  %>
        <style type="text/css">
        
        </style>
        <title>我的帖子</title>
    </head>
    <body>
		<% include('/common/common_body_header.html',{active:'none'}){} %>
		
		
		<div class="container-fluid">
			<div class="row">
				<div class="col-md-2 col-md-offset-1" >
					<% include('/common/common_user_menu.html',{active:'post'}){} %>
				</div>
				
				<div class="col-md-8">
					
					<form id="form" onsubmit="return false;">
						<table class="table table-hover">
							<thead>
								<tr>
									<th><input type="checkbox"/></th>
									<th>标题</th>
									<th>标签</th>
									<th>发布时间</th>
									<th>操作</th>
								</tr>
							</thead>
							<tbody>
							
								<% for(var post in posts){ %>
									<tr>
										<td><input name="postId" type="checkbox" value="${post.postId}"/></td>
										<td><a target="_blank" href="${ctxPath}/post/${post.postId}">${post.title}</a></td>
										<td>
			                                <% if(post.type == 'DISCUSS'){ %>
			                                	<a href="${ctxPath}/discuss" target="_blank"><span class="label label-success">技术交流</span></a>
											<%  } %>
			                               	
			                               	<% if(post.type == 'HELP'){ %>
			                               		<a href="${ctxPath}/help" target="_blank"><span class="label label-success">求助问答</span></a>
			                               	<%  } %>
			                               	
			                               	<% if(post.type == 'LEISURE'){ %>
			                               		<a href="${ctxPath}/leisure" target="_blank"><span class="label label-success">闲聊灌水</span></a>
											<%  } %>
			                               	
			                               	<% if(post.type == 'NOTICE'){ %>
			                               		<a href="${ctxPath}/notice" target="_blank"><span class="label label-success">系统公告</span></a>
			                               	<%  } %>
			                               	
			                               	<% if(post.type == 'SUGGEST'){ %>
			                               		<a href="${ctxPath}/suggest" target="_blank"><span class="label label-success">建议/BUG</span></a>
											<%  } %>
											
											<% if(post.anonymous){ %>
			                                	<a href="javascript:void(0);" target="_blank"><span class="label label-default">匿名</span></a>
											<%  } %>
											
											<% if(post.top){ %>
			                                	<a href="javascript:void(0);" target="_blank"><span class="label label-danger">置顶</span></a>
											<%  } %>
			                               	
										</td>
										<td>${post.createDate,"yyyy-MM-dd HH:mm:ss"}</td>
										<td>
											<button class="btn-update btn btn-xs btn-primary" data-options="${post.postId}">编辑</button>
											<button class="btn-delete btn btn-xs btn-danger" data-options="${post.postId}">删除</button>
										</td>
									</tr>
								<% } %>
							</tbody>
						</table>
					</form>
					
					<div style="text-align: right;">
						<button id="batch-delete" class="btn btn-danger">批量删除</button>
					</div>
						
					<%//分页开始%>
					<div style="text-align: center;">
						
						<ul class="pagination ">
							<%
								var begin = 0;
								var end = 0;
								if(pageInfo.totalPage <= 10){
									begin = 1;
									end = pageInfo.totalPage;
								}else{
									begin = pageInfo.page - 5;
									end = pageInfo.page + 4;
									if(begin < 1){
										begin = 1;
										end = 10;
									}
									if(end > pageInfo.totalPage){
										begin = pageInfo.totalPage - 9;
										end = pageInfo.totalPage;
									}	
								}
							%>
							
							<% if(begin > 1){%>
								<li><a href="${ctxPath}/user/post?page=1">首页</a></li>
							<%}%>
							
							<% if(pageInfo.page > 1){ %>
								<li><a href="${ctxPath}/user/post?page=${pageInfo.page - 1}">上一页</a></li>
							<%}%>
							
							<% 	for(var i = begin ; i <= end; i++){ %>
									<li ${pageInfo.page == i ? 'class="active"'}><a href="${pageInfo.page == i ? 'javascript:void(0);':ctxPath + '/user/post?page=' + i} " >${i}</a></li>
							<% }	%>
							 
							 <% if(pageInfo.totalPage > pageInfo.page){ %>
								<li><a href="${ctxPath}/user/post?page=${pageInfo.page + 1}">下一页</a></li>
							<%}%>
							
							<% if(pageInfo.totalPage > end ){%>
								<li><a href="${ctxPath}/user/post?page=${pageInfo.totalPage}">尾页</a></li>
							<%}%>
						</ul>
					</div>
					<%//分页结束 %>
				</div>
			</div>
		</div>
		
		
		<% include('/common/common_footer.html',{}){} %>
    </body>
	<% include('/common/common_script.html',{}){} %>
    <script>
	    $(function () {
	    	$('.btn-delete').click(function(){
	    		if($(this).hasClass('disabled')){
					return;
				}
	    		
	    		var btn = this;
	    		
	    		layer.confirm('确定要删除吗?',{
						btn: ['确定','取消']
	    			}, function(index){
	    				$(btn).addClass('disabled');
	        			$(btn).text('执行中...')
						//layer.close(index)
						layer.load(2);
	    				$.ajax({
	    	    			url:'/post/delete',
	    	    			method:'POST',
	    	    			data:'postId=' + $(btn).attr('data-options'),
	    	    			dataType:'JSON',
	    	    			success:function(response){
	    	    				if(response.success){
	    	    					window.location.reload();
	    	    				}else{
	    	    					layer.closeAll();
	    	    					layer.msg(response.message);
	    	    					$(btn).removeClass('disabled');
	    		        			$(btn).text('删除');
	    	    				}
	    	    			}
	    	    		});
	    			}, function(){
				});
	    		
	    	});
	    	
			$('.btn-update').click(function(){
	    		window.location.href = "${ctxPath}/post/update/" + $(this).attr('data-options');
	    	});

	    	$(":checkbox:first").click(function(i){
	    		$('[name=postId]:checkbox').prop('checked', this.checked);
	    	});
	    	
	    	$('#batch-delete').click(function(){
	    		if($(this).hasClass('disabled')){
					return;
				}
	    		var form = $('#form').serializeArray();
	    		if(form.length == 0){
	    			layer.msg('请选择需要删除的记录');
	    			return;
	    		}
	    		layer.confirm('确定要删除吗?',{
						btn: ['确定','取消']
	    			}, function(index){
	    				$('#batch-delete').addClass('disabled');
	        			$('#batch-delete').text('执行中... ...')
						//layer.close(index)
						layer.load(2);
	    				$.ajax({
	    	    			url:'/post/delete',
	    	    			method:'POST',
	    	    			data:form,
	    	    			dataType:'JSON',
	    	    			success:function(response){
	    	    				if(response.success){
	    	    					window.location.reload();
	    	    				}else{
	    	    					layer.closeAll();
	    	    					layer.msg(response.message);
	    	    					$('#batch-delete').removeClass('disabled');
	    		        			$('#batch-delete').text('批量删除');
	    	    				}
	    	    			}
	    	    		});
	    			}, function(){
    			});
	    	});
	    });
    </script>
</html>